var self;
mui.init({
	beforeback: function() {
		var parent = plus.webview.currentWebview().opener();
		mui.fire(parent, "reload");
	}
});
var myVar;
var currenttime;
var flag = false;
mui.plusReady(function() {
	self = plus.webview.currentWebview();
	initListener();
	initCs();
});

var accessToken = "at.5m2cm7mr5lnbmk6f5iam3f98487t9v7q-7slaie3hdg-0gts6vq-cuot9zcnj";
var deviceSerial = "240534926";
var channelNo = 1;

var datayt = {
	accessToken: accessToken,
	deviceSerial: deviceSerial,
	channelNo: channelNo,
	speed: 2
}

function initListener() {
	$(".icon_back").on("tap", function() {
		mui.back();
	})
	$("#luxiang").on("tap", function() {
		var player = document.getElementById('ysOpenDevice').contentWindow;
		if(flag) {
			//打开音乐
			flag = false;
		} else {
			flag = true;
		}

	})
}

var control = function(direction) {
	datayt = {
		accessToken: accessToken,
		deviceSerial: deviceSerial,
		channelNo: channelNo,
		speed: 2
	}
	datayt.direction = parseInt(direction); //控制方式,我传了0,1,2,3,8,9
	ajaxF2("https://open.ys7.com/api/lapp/device/ptz/start", datayt, function(res) {
		if(res.code == 200) {
			mui.toast("操作成功")
			stop();
		} else if(res.code == 10002) {
			mui.toast("token过期")
			getRemoteToken();
		}
	}, function(res) {
		console.log(JSON.stringify(res))

	})

}

function getRemoteToken() {
	var param = {
		appKey: "2f740be8db884cfca2e6eaa5c3aafd26",
		appSecret: "75c4fb3d47a48328b774d78aebbd21e5"
	}
	ajaxF2("https://open.ys7.com/api/lapp/token/get", param, function(res) {
		if(res.code == 200) {
			accessToken = res.data.accessToken;
			console.log(accessToken)
			updateToken(accessToken);

		}
	}, function(res) {
		console.log(JSON.stringify(res))

	})
}
// 设置Token
function updateToken(token) {
	ajaxF1(UPDATETOKEN, {
		accesstoken: token,
		jbzid: self.guid
	}, function(result) {

	}, function() {
		mui.toast("token更新失败");
	});

}

//停止转动
var stop = function() {
	datayt = {
		accessToken: accessToken,
		deviceSerial: deviceSerial,
		channelNo: channelNo,
		speed: 2
	}
	ajaxF2("https://open.ys7.com/api/lapp/device/ptz/stop", datayt, function(res) {
		if(res.code == 200) {
			//			decoder = new EZUIKit.EZUIPlayer({
			//				id: 'myPlayer',
			//				autoplay: true,
			//				url: "ezopen://open.ys7.com/" + deviceSerial + "/1.hd.live",
			//				accessToken: accessToken,
			//				decoderPath: 'http://gege.lvjing.site:8080/spjm/js',
			//				width: 600,
			//				height: 400,
			//				handleError: handleError,
			//				handleSuccess: handleSuccess,
			//			});
			//			decoder.openSound();
		}
	})

}

var decoder;

//jianyantoken 是否过期过期updatToken
var stop1 = function() {
	datayt = {
		accessToken: accessToken,
		deviceSerial: deviceSerial,
		channelNo: channelNo,
		speed: 2
	}
	ajaxF2("https://open.ys7.com/api/lapp/device/ptz/stop", datayt, function(res) {
		if(res.code == 200) {
			decoder = new EZUIKit.EZUIPlayer({
				id: 'myPlayer',
				autoplay: false,
				url: "ezopen://open.ys7.com/240534926/1.hd.live",
				accessToken: accessToken,
				decoderPath: 'http://gege.lvjing.site:8080/spjm/js',
			});
			decoder.openSound();
		} else if(res.code == 10002) {
			getRemoteToken();
		}
	})

}

function initCs() {
	//初始化参数;
	try {
		ajaxF1(GETSXT, {
			// 改为施工处ID字段名字保持不变
			jbzid: self.SGCID
		}, function(result) {
			if(result.successful) {
				data = result.resultValue;
				if(data.length > 0) {
					accessToken = data[0].ACCESSTOKEN;
					deviceSerial = data[0].DEVICESERIAL;
					channelNo = data[0].CHANNELNO;
					var ZBID = data[0].ZHIBOID; // 直播ID
//					var rtmp = 'rtmp://rtmp01open.ys7.com/openlive/' + ZBID + ".hd";
//					var hls = "http://hls01open.ys7.com/openlive/" + ZBID + ".hd.m3u8";
//					$("#myPlayer").append('<source id="rtmp_video"  src="' + rtmp + '"  type="" />');
//					$("#myPlayer").append('<source  id="hls_video"   src="' + hls + '"   type="application/x-mpegURL" />');
//				    
                   var ljh="https://open.ys7.com/ezopen/h5/iframe_se?url=ezopen://open.ys7.com/"+deviceSerial +"/1.live&autoplay=1&accessToken="+accessToken;
                   $("iframe").attr("src",ljh)
				}
			}
		}, function() {
			mui.toast("请求失败");

		});
	} catch(e) {

	}
}